
/**
 * shengdai 流程引擎 js 
 * 
 * Author shengdai
 * version 0.5
 * date	2012-08-12
 * **/
$.sdAFWorkflow = function(options) {
		this.settings = $.extend(true, {}, $.sdAFWorkflow.defaults, options );
		this.initParas();
		this.init();
};
$.extend($.sdAFWorkflow, {
		defaults : {
			targetObj : null,//调用时的源目标对象
			onBefore : function(){return true;},//流程发起前事件，拦截判断用
			onAfter : function(){return true;},//提交后事件
			onSuccessAfter : function(){return true;},//执行成功后事件
			isStart : false,//是否刚开始发起
			startUrl : baseURL+"/process/gtasks/submit/beforeStart.do",//流程发起url
			nextUrl : baseURL+"/process/gtasks/submit/beforeNext.do",//流程提交至下一步url
			parms : {
				routeId : $("#routeId").val(),
				boId : $("#boId").val(),
				mainBoId : $("#mainBoId").val(),
				worklistId : $("#worklistId").val(),
				busiType : $("#busiType").val(),
				flowId : $("#flowId").val(),
				parentNodeInstanceId : $("#parentNodeInstanceId").val(),
				diyNextUrl : baseURL+"/process/gtasks/submit/beforeNext.do"
			}
		},
		prototype: {
			initParas : function(){
				var _this = this;
				if(typeof(_this.settings.targetObj) != 'undefined' &&  _this.settings.targetObj != null) _this.settings.parms.routeId = $(_this.settings.targetObj).attr("routeid");
				if(typeof(_this.settings.parms.routeId) == 'undefined' || _this.settings.parms.routeId=='') _this.settings.parms.routeId = $("#routeId").val();
				if(typeof(_this.settings.parms.boId) == 'undefined'  || _this.settings.parms.boId=='') _this.settings.parms.boId = $("#boId").val();
				if(typeof(_this.settings.parms.mainBoId) == 'undefined'  || _this.settings.parms.mainBoId=='') _this.settings.parms.mainBoId = $("#mainBoId").val();
				if(typeof(_this.settings.parms.worklistId) == 'undefined'  || _this.settings.parms.worklistId=='') _this.settings.parms.worklistId = $("#worklistId").val();
				if(typeof(_this.settings.parms.busiType) == 'undefined'  || _this.settings.parms.busiType=='') _this.settings.parms.busiType = $("#busiType").val();
				if(typeof(_this.settings.parms.flowId) == 'undefined'  || _this.settings.parms.flowId=='') _this.settings.parms.flowId = $("#flowId").val();
				if(typeof(_this.settings.parms.parentNodeInstanceId) == 'undefined'  || _this.settings.parms.parentNodeInstanceId=='') _this.settings.parms.parentNodeInstanceId = $("#parentNodeInstanceId").val();
				if(typeof(_this.settings.parms.diyNextUrl) != 'undefined'  && _this.settings.parms.diyNextUrl!='') _this.settings.nextUrl = _this.settings.parms.diyNextUrl;
			},
			init: function() {
				var _this = this;
				if(!_this.settings.onBefore(_this)) return false;
				//根据isStart选择流程下一步指向的url
  				var url = _this.settings.isStart ? _this.settings.startUrl : _this.settings.nextUrl;
  				url += "?"+$.param(_this.settings.parms);
/*				$.ligerDialog.open({//弹出意见填写框，且可选择人
  					url: url,
  					height: 400,
  					width: 500,
  					name: 'opinionFrame',
  					title : "流程提交对话框",
  					buttons : [{text: '提交', 
  						onclick: function (item, dialog) {
  							$(".l-dialog-win").find(".l-dialog-btn").attr("style","display:none");
  							dialog.frame.FF_Submit(function(data){
  								if(data && data.hasOk){
  									dialog.close();
  									_this.settings.onSuccessAfter(_this);//调用提交后的事件
  								}
  								$(".l-dialog-win").find(".l-dialog-btn").attr("style","display:block");
  							}, _this.settings.isStart);
  							_this.settings.onAfter(_this);//调用提交后的事件
  						} 
  				    }, 
  					{ text: '取消', 
  						onclick: function (item, dialog) { 
  							_this.settings.onAfter(_this);//调用提交后的事件
  							dialog.close(); } } 
  				    ]
  				});*/
  				BootstrapDialog.show({
  					title: '提交流程对话框',
  					closable: false,
  				    message: function(dialog) {//渲染对话框页面
  				        var $message = $('<div></div>');
  				        var pageToLoad = dialog.getData('pageToLoad');
  				        $message.load(pageToLoad);
  				        return $message;
  				    },
  				    data: {
  				        'pageToLoad': url
  				    },
  			        buttons: [{
			            label: '提交',
			            cssClass: 'btn-primary',
			            action: function (dialog) {
  							//提交事件，提交完成后关闭当前页面、执行onSuccessAfter事件
  			            	FF_Submit(function(data){
  								if(data && data.hasOk){
  									dialog.close();
  									_this.settings.onSuccessAfter(_this);
  								}
  								$(".l-dialog-win").find(".l-dialog-btn").attr("style","display:block");
  							}, _this.settings.isStart);
  							_this.settings.onAfter(_this);//调用提交后的事件
		            	} 
					},{
					    label: '取消',
					    action: function(dialogItself){
					        dialogItself.close();
					    }
					}]
  				});
			}
		}
});
(function($){
	$.extend($.fn, {
		sdAFWorkflow : function(options){
			return new $.sdAFWorkflow(options);
		}
	});
});
//重新选人
function FF_Reselect(options){
	$(this).addressBook(options);
}